import { ipcMain, app, BrowserWindow, Menu, Tray } from 'electron'
import path from 'path';
import '../renderer/store'
let tray = null;
/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

let mainWindow
let onlineWin
const winURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:9080`
  : `file://${__dirname}/index.html`
let contextMenu = Menu.buildFromTemplate([
  { label: '显示主界面', click: () => { mainWindow.show() } },
  { label: '未连接' },
  { label: '在线客服', click: () => { mainWindow.webContents.send("show-online") } },
  // { label: '检查更新', click: () => { mainWindow.webContents.send("is-update") } },
  { label: '退出软件', click: () => {mainWindow.webContents.send("quit-app") } },//我们需要在这里有一个真正的退出（这里直接强制退出）open-online
])
let contextMenu2 = Menu.buildFromTemplate([
  { label: '显示主界面', click: () => { mainWindow.show() } },
  { label: '断开连接', click: () => { mainWindow.webContents.send("disconnect") } },
  { label: '在线客服', click: () => { mainWindow.webContents.send("show-online") } },
  // { label: '检查更新', click: () => { mainWindow.webContents.send("is-update") } },
  { label: '退出软件', click: () => { mainWindow.webContents.send("quit-app")} },//我们需要在这里有一个真正的退出（这里直接强制退出）
])
function createWindow() {
  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    height: 560,
    useContentSize: true,
    width: 500,
    frame: false,
    webPreferences: {
      webSecurity: false
    }
  })
  Menu.setApplicationMenu(null)
  mainWindow.loadURL(winURL)
  tray = new Tray(`${__static}/logo.png`);
  tray.setToolTip('熊猫加速器')
  tray.setContextMenu(contextMenu)
  tray.on('click', () => { //我们这里模拟桌面程序点击通知区图标实现打开关闭应用的功能
    mainWindow.isVisible() ? mainWindow.hide() : mainWindow.show()
    mainWindow.isVisible() ? mainWindow.setSkipTaskbar(false) : mainWindow.setSkipTaskbar(true);
  })
  mainWindow.on('show', () => {
    tray.setHighlightMode('always')
  })
  mainWindow.on('hide', () => {
    tray.setHighlightMode('never')
  })
  mainWindow.on('closed', () => {
    mainWindow = null
  })
}
function openPay() {
  var payWin = new BrowserWindow({
    width: 770,
    height: 800,
    parent: mainWindow, // win是主窗口
    // frame:false,
    webPreferences: {
      nodeIntegration: true
    }
  })
  payWin.loadURL(winURL + '#/gopay');
  payWin.on('closed', () => { payWin = null })
}
function openOnline(onLineUrl) {
  console.log(onlineWin)
  // if (onlineWin != null) {
  //   onlineWin.show()
  // } else {
  onlineWin = null
  onlineWin = new BrowserWindow({
    width: 420,
    height: 680,
    parent: mainWindow, // win是主窗口
    show: false,
    resizable: false,
    minimizable: false,
    webPreferences: {
      nodeIntegration: true
    }
  })
  // onlineWin.loadURL(winURL + '#/openOnline');
  onlineWin.loadURL(onLineUrl);
  onlineWin.once('ready-to-show', () => {
    onlineWin.show()
  })
  onlineWin.on('chat-closed', () => { onlineWin = null })
  // }

}
app.on('ready', createWindow)
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})
//打开支付窗口
ipcMain.on('open-pay', () => {
  openPay()
})
//打开在线客服窗口
ipcMain.on('open-online', (e, arg) => {
  openOnline(arg)
})
ipcMain.on('window-min', () => {
  mainWindow.minimize()
})
//获取连接状态
ipcMain.on('concat-status', (e, arg) => {
  console.log('concat status----',arg)
  if (arg == true) {
      tray.setContextMenu(contextMenu2)
  }
})
//断开连接
ipcMain.on('dis-connect', (e, arg) => {
  tray.setContextMenu(contextMenu)
})
ipcMain.on('window-close', () => {
  mainWindow.hide()
})
ipcMain.on('quitApp',()=>{
  mainWindow.close()
})
/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */


// import { autoUpdater } from 'electron-updater'

// autoUpdater.on('update-downloaded', () => {
//   autoUpdater.quitAndInstall()
// })

// app.on('ready', () => {
//   if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
// })
